package com.lixixi.tingshu.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.lixixi.tingshu.album.api.AlbumApiFeignClient;
import com.lixixi.tingshu.common.constant.SystemConstant;
import com.lixixi.tingshu.common.result.Result;
import com.lixixi.tingshu.model.user.UserInfo;
import com.lixixi.tingshu.model.user.UserPaidAlbum;
import com.lixixi.tingshu.model.user.UserPaidTrack;
import com.lixixi.tingshu.model.user.UserVipService;
import com.lixixi.tingshu.user.mapper.UserInfoMapper;
import com.lixixi.tingshu.user.service.*;
import com.lixixi.tingshu.vo.user.UserInfoVo;
import com.lixixi.tingshu.vo.user.UserPaidRecordVo;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.LinkedList;
import java.util.List;

public class UserRecordServiceImpl implements UserRecordService {

	@Autowired
	UserPaidTrackService userPaidTrackService;

	@Autowired
	UserPaidAlbumService userPaidAlbumService;

	@Autowired
	UserVipServiceService userVipServiceService;

	@Autowired
	AlbumApiFeignClient albumApiFeignClient;

	@Autowired
	UserInfoMapper userInfoMapper;

	@Transactional
	@Override
	public void recordItem(UserPaidRecordVo userPaidRecordVo) {
		//获取项目类型
		String itemType = userPaidRecordVo.getItemType();
		//获取商品id列表
		List<Long> itemIdList = userPaidRecordVo.getItemIdList();
		//获取订单号
		String orderNo = userPaidRecordVo.getOrderNo();
		//获取用户id
		Long userId = userPaidRecordVo.getUserId();
		if (SystemConstant.ORDER_ITEM_TYPE_ALBUM.equals(itemType))
		{
			//查询是否已存在专辑,防止消息重复消费
			long count = userPaidAlbumService.count(new LambdaQueryWrapper<UserPaidAlbum>()
					.eq(UserPaidAlbum::getUserId, userId)
					.eq(UserPaidAlbum::getOrderNo, orderNo));
			if (count > 0)
			{
				//重复消费, 直接返回
				return;
			}
			UserPaidAlbum userPaidAlbum = new UserPaidAlbum();
			userPaidAlbum.setUserId(userId);
			userPaidAlbum.setOrderNo(orderNo);
			userPaidAlbum.setAlbumId(itemIdList.get(0));
			userPaidAlbumService.save(userPaidAlbum);
		} else if (SystemConstant.ORDER_ITEM_TYPE_TRACK.equals(itemType))
		{
			//查询是否已存在声音,防止消息重复消费
			long count = userPaidTrackService.count(new LambdaQueryWrapper<UserPaidTrack>()
					.eq(UserPaidTrack::getUserId, userId)
					.eq(UserPaidTrack::getOrderNo, orderNo));
			if (count > 0)
			{
				//重复消费, 直接返回
				return;
			}
			// 使用列表批量保存
			LinkedList<UserPaidTrack> list = new LinkedList<>();
			//查询声音信息
			Long albumId = albumApiFeignClient.getAlbumIdByTrackId(itemIdList.get(0)).getData();
			itemIdList.forEach(itemId -> {
				UserPaidTrack userPaidTrack = new UserPaidTrack();
				userPaidTrack.setUserId(userId);
				userPaidTrack.setOrderNo(orderNo);
				userPaidTrack.setTrackId(itemId);
				userPaidTrack.setAlbumId(albumId);
				list.add(userPaidTrack);
			});
			userPaidTrackService.saveBatch(list);
		} else
		{
			//查询是否已存在Vip服务,防止消息重复消费
			long count = userVipServiceService.count(new LambdaQueryWrapper<UserVipService>()
					.eq(UserVipService::getUserId, userId)
					.eq(UserVipService::getOrderNo, orderNo));
			if (count > 0)
			{
				//重复消费, 直接返回
				return;
			}
			//获取用户信息
			UserInfo userInfo = userInfoMapper.selectById(userId);
			//获取用户身份
			Integer isVip = userInfo.getIsVip();
			//获取用户vip过期时间
			Date vipExpireTime = userInfo.getVipExpireTime();
			DateTime dateTime;
			if (SystemConstant.VIP.equals(isVip) && vipExpireTime.after(new Date()))
			{
				//判断服务类型
				dateTime = new DateTime(vipExpireTime);
				Long itemId = itemIdList.get(0);
				if (itemId.equals(1L))
				{
					dateTime.plusMonths(1);
				} else if (itemId.equals(2L))
				{
					dateTime.plusMonths(3);
				} else
				{
					dateTime.plusYears(1);
				}

			} else
			{
				//vip已过期或不是vip用户
				//判断vip服务类型
				dateTime = new DateTime();
				Long itemId = itemIdList.get(0);
				if (itemId.equals(1L))
				{
					dateTime.plusMonths(1);
				} else if (itemId.equals(2L))
				{
					dateTime.plusMonths(3);
				} else
				{
					dateTime.plusYears(1);
				}
			}
			//更新vip权限
			userInfo.setIsVip(SystemConstant.VIP);
			userInfo.setVipExpireTime(dateTime.toDate());
			userInfo.setUpdateTime(new Date());
			//记录vip服务
			UserVipService userVipService = new UserVipService();
			userVipService.setUserId(userId);
			userVipService.setOrderNo(orderNo);
			userVipService.setExpireTime(dateTime.toDate());
			userVipService.setStartTime(new Date());
			userVipServiceService.save(userVipService);
			//更新用户权限
			userInfoMapper.updateById(userInfo);
		}
	}
}
